﻿namespace LeetCodeProject._0001_1000._101_200._111_120._114;

public class Solution
{
    public void Flatten(TreeNode root)
    {
        if (root == null)
        {
            return;
        }

        Stack<TreeNode> stk = new Stack<TreeNode>();
        stk.Push(root);
        TreeNode last = null;
        while (stk.Count > 0)
        {
            root = stk.Pop();
            if (root.right != null)
            {
                stk.Push(root.right);
            }

            if (root.left != null)
            {
                stk.Push(root.left);
            }

            if (last != null)
            {
                last.right = root;
                last.left = null;
            }

            last = root;
        }
    }
}